<!-- 头部吸顶导航 -->
<script setup>
import { onMounted, ref } from "vue";
import { findAllCategory } from "@/api/category";
import store from "@/store";

let list = ref(store.state.category.list);

findAllCategory().then((res) => {
  store.commit("setList", res);
  list.value = res;
});

let y = ref(0);

onMounted(() => {
  window.onscroll = () => {
    const scorllTop = document.documentElement.scrollTop;
    y.value = scorllTop;
  };
});
</script>

<template>
  <div class="app-header-sticky" :class="{ show: y >= 185 }">
    <div class="container" v-show="y >= 85">
      <RouterLink class="logo" to="/" />
      <ul class="app-header-nav">
        <li class="home">
          <RouterLink to="/">首页</RouterLink>
        </li>
        <li v-for="item in list" :key="item.id">
          <router-link :to="`/category/${item.id}`">{{
            item.name
          }}</router-link>
          <div class="layer">
            <ul>
              <li v-for="sub in item.children" :key="sub.id">
                <router-link :to="`/category/sub/${sub.id}`">
                  <img :src="sub.picture" alt />
                  <p>{{ sub.name }}</p>
                </router-link>
              </li>
            </ul>
          </div>
        </li>
      </ul>
      <div class="right">
        <RouterLink to="/">品牌</RouterLink>
        <RouterLink to="/">专题</RouterLink>
      </div>
    </div>
  </div>
</template>

<style lang='less' scoped>
.app-header-sticky {
  width: 100%;
  height: 80px;
  position: fixed;
  left: 0;
  top: 0;
  z-index: 999;
  background-color: #fff;
  border-bottom: 1px solid #e4e4e4;
  transform: translateY(-100%);
  opacity: 0;
  &.show {
    transition: all 0.3s linear;
    transform: none;
    opacity: 1;
  }
  .container {
    display: flex;
    align-items: center;
    justify-content: space-around;
  }
  .logo {
    width: 200px;
    height: 75px;
    background: url(https://img.zcool.cn/community/01de41556ecea20000009fcb700342.jpg@1280w_1l_2o_100sh.jpg)
      no-repeat center 0px / contain;
  }
  .right {
    width: 220px;
    display: flex;
    text-align: center;
    padding-left: 40px;
    border-left: 2px solid @xtxColor;
    a {
      width: 38px;
      margin-right: 40px;
      font-size: 16px;
      line-height: 1;
      &:hover {
        color: @xtxColor;
      }
    }
  }
}

.app-header-nav {
  width: 820px;
  display: flex;
  justify-content: space-around;
  padding-left: 40px;
  position: relative;
  z-index: 998;
  > li {
    margin-right: 40px;
    width: 38px;
    text-align: center;
    > a {
      font-size: 16px;
      line-height: 32px;
      height: 32px;
      display: inline-block;
    }
    &:hover {
      > a {
        color: @xtxColor;
        border-bottom: 1px solid @xtxColor;
      }
      > .layer {
        height: 132px;
        opacity: 1;
      }
    }
  }
}
.layer {
  width: 1240px;
  background-color: #fff;
  position: absolute;
  left: -200px;
  top: 56px;
  height: 0;
  overflow: hidden;
  opacity: 0;
  border-radius: 3px;
  box-shadow: 0 0 5px #ccc;
  transition: all 0.2s 0.1s;
  ul {
    display: flex;
    flex-wrap: wrap;
    padding: 0 70px;
    align-items: center;
    height: 132px;
    li {
      width: 110px;
      text-align: center;
      img {
        width: 60px;
        height: 60px;
      }
      p {
        padding-top: 10px;
      }
      &:hover {
        p {
          color: @xtxColor;
        }
      }
    }
  }
}
</style>